/** 动态菜单 */
import { ref } from "vue";
import { defineFakeRoute } from "vite-plugin-fake-server/client";

// 为了方便进行调试，这里使用了树形结构进行编写
// 但实际输出的数据仍然是列表
const menus = ref([
  {
    id: 111,
    parentId: 0,
    type: "menu",
    menuName: "常规模板",
    routeName: "Template",
    path: "/template",
    component: "",
    frameSrc: "",
    redirect: "/template/table",
    icon: "ep/message-box",
    sort: 5,
    keepAlive: true,
    hiddenTag: false,
    fixedTag: false,
    showLink: true,
    showParent: true,
    remark: "The Eighth Life",
    status: true,
    children: [
      {
        id: 222,
        parentId: 111,
        type: "menu",
        menuName: "表格模板",
        routeName: "TemplateTable",
        path: "/template/table",
        component: "template/table/index",
        frameSrc: "",
        redirect: "",
        icon: "ri/table-line",
        sort: 10,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "Thursday Next Series",
        status: true,
        children: []
      },
      {
        id: 333,
        parentId: 111,
        type: "menu",
        menuName: "配置模板",
        routeName: "TemplateSetting",
        path: "/template/setting",
        component: "template/setting/index",
        frameSrc: "",
        redirect: "",
        icon: "ri/table-line",
        sort: 10,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "Thursday Next Series",
        status: true,
        children: []
      }
    ]
  },
  {
    id: 1,
    parentId: 0,
    type: "menu",
    menuName: "系统管理",
    routeName: "System",
    path: "/system",
    component: "",
    frameSrc: "",
    redirect: "/system/user",
    icon: "ri/settings-3-line",
    sort: 10,
    keepAlive: true,
    hiddenTag: false,
    fixedTag: false,
    showLink: true,
    showParent: true,
    remark: "The Eighth Life",
    status: true,
    children: [
      {
        id: 11,
        parentId: 1,
        type: "menu",
        menuName: "用户管理",
        routeName: "SystemUser",
        path: "/system/user",
        component: "system/user/index",
        frameSrc: "",
        redirect: "",
        icon: "ri/admin-line",
        sort: 10,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "Thursday Next Series",
        status: true,
        children: []
      },
      {
        id: 12,
        parentId: 1,
        type: "menu",
        menuName: "角色管理",
        routeName: "SystemRole",
        path: "/system/role",
        component: "system/role/index",
        frameSrc: "",
        redirect: "",
        icon: "ri/admin-fill",
        sort: 20,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "The Wheel of Time",
        status: true,
        children: []
      },
      {
        id: 13,
        parentId: 1,
        type: "menu",
        menuName: "菜单管理",
        routeName: "SystemMenu",
        path: "/system/menu",
        component: "system/menu/index",
        frameSrc: "",
        redirect: "",
        icon: "ep/menu",
        sort: 30,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "The Dresden Files",
        status: true,
        children: []
      },
      {
        id: 14,
        parentId: 1,
        type: "menu",
        menuName: "部门管理",
        routeName: "SystemDept",
        path: "/system/dept",
        component: "system/dept/index",
        frameSrc: "",
        redirect: "",
        icon: "ri/git-branch-line",
        sort: 40,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "Twilight",
        status: true,
        children: []
      }
    ]
  },
  {
    id: 2,
    parentId: 0,
    type: "menu",
    menuName: "测试路由",
    routeName: "Testing",
    path: "/testing",
    component: "",
    frameSrc: "",
    redirect: "/testing/permission",
    icon: "ri/test-tube-line",
    sort: 100,
    keepAlive: true,
    hiddenTag: false,
    fixedTag: false,
    showLink: true,
    showParent: true,
    remark: "Percy Jackson",
    status: true,
    children: [
      {
        id: 21,
        parentId: 2,
        type: "menu",
        menuName: "权限测试",
        routeName: "TestingPermission",
        path: "/testing/permission",
        component: "testing/permission",
        frameSrc: "",
        redirect: "",
        icon: "",
        sort: 10,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "Discworld",
        status: true,
        children: [
          {
            id: 5,
            parentId: 21,
            type: "button",
            menuName: "添加",
            routeName: "backend:btn:add",
            path: "",
            component: "",
            frameSrc: "",
            redirect: "",
            icon: "",
            sort: 10,
            keepAlive: true,
            hiddenTag: false,
            fixedTag: false,
            showLink: true,
            showParent: true,
            remark: "The Infinity Cycle",
            status: true,
            children: []
          },
          {
            id: 6,
            parentId: 21,
            type: "button",
            menuName: "修改",
            routeName: "backend:btn:edit",
            path: "",
            component: "",
            frameSrc: "",
            redirect: "",
            icon: "",
            sort: 20,
            keepAlive: true,
            hiddenTag: false,
            fixedTag: false,
            showLink: true,
            showParent: true,
            remark: "War and Peace",
            status: true,
            children: []
          },
          {
            id: 7,
            parentId: 21,
            type: "button",
            menuName: "删除",
            routeName: "backend:btn:delete",
            path: "",
            component: "",
            frameSrc: "",
            redirect: "",
            icon: "",
            sort: 30,
            keepAlive: true,
            hiddenTag: false,
            fixedTag: false,
            showLink: true,
            showParent: true,
            remark: "The Hunger Games",
            status: true,
            children: []
          }
        ]
      },
      {
        id: 22,
        parentId: 2,
        type: "menu",
        menuName: "admin权限",
        routeName: "TestingAdmin",
        path: "/testing/admin",
        component: "testing/admin",
        frameSrc: "",
        redirect: "",
        icon: "",
        sort: 20,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "The Chronicles of Narnia",
        status: true,
        children: []
      },
      {
        id: 23,
        parentId: 2,
        type: "menu",
        menuName: "关于",
        routeName: "TestingAbout",
        path: "/testing/about",
        component: "testing/about",
        frameSrc: "",
        redirect: "",
        icon: "",
        sort: 40,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "Percy Jackson",
        status: true,
        children: []
      },
      {
        id: 24,
        parentId: 2,
        type: "menu",
        menuName: "common权限",
        routeName: "TestingCommon",
        path: "/testing/common",
        component: "testing/common",
        frameSrc: "",
        redirect: "",
        icon: "",
        sort: 30,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "Anna Karenina",
        status: true,
        children: []
      },
      {
        id: 25,
        parentId: 2,
        type: "menu",
        menuName: "组件测试",
        routeName: "TestingComponent",
        path: "/testing/component",
        component: "testing/component",
        frameSrc: "",
        redirect: "",
        icon: "",
        sort: 5,
        keepAlive: true,
        hiddenTag: false,
        fixedTag: false,
        showLink: true,
        showParent: true,
        remark: "The Southern Reach",
        status: true,
        children: []
      }
    ]
  }
]);

// 后端路由范本（择期移除）
//
// const asyncRouter = {
//   name: "Testing",
//   path: "/testing",
//   meta: {
//     title: "测试路由",
//     icon: "ri/test-tube-line",
//     rank: 100
//   },
//   children: [
//     {
//       path: "/testing/component",
//       component: "testing/component",
//       name: "TestingComponent",
//       meta: {
//         title: "组件测试"
//       }
//     },
//     {
//       path: "/testing/permission",
//       component: "testing/permission",
//       name: "TestingPermission",
//       meta: {
//         title: "权限测试",
//         auths: ["backend:btn:add", "backend:btn:edit", "backend:btn:delete"]
//       }
//     },
//     {
//       path: "/testing/admin",
//       component: "testing/admin",
//       name: "TestingAdmin",
//       meta: {
//         title: "admin权限",
//         roles: ["admin"]
//       }
//     },
//     {
//       path: "/testing/common",
//       component: "testing/common",
//       name: "TestingCommon",
//       meta: {
//         title: "common权限",
//         roles: ["common"]
//       }
//     },
//     {
//       path: "/testing/about",
//       component: "testing/about",
//       name: "TestingAbout",
//       meta: {
//         title: "关于"
//       }
//     }
//   ]
// };

export function getAsyncMenus(): any[] {
  const result: any[] = [];
  const stack: any[] = [...menus.value];

  while (stack.length > 0) {
    const node = stack.pop()!;
    const flatNode: any = { ...node };
    delete flatNode.children;
    result.push(flatNode);

    // 将子节点逆序推入栈中，以保持原始顺序
    if (node.children && node.children.length > 0) {
      for (let i = node.children.length - 1; i >= 0; i--) {
        stack.push(node.children[i]);
      }
    }
  }

  // sort排序
  result.sort((a, b) => a.sort - b.sort);

  return result;
}

export default defineFakeRoute([
  {
    url: "/access/routes",
    method: "get",
    response: () => {
      return {
        success: true,
        message: "操作成功",
        //data: [asyncRouter],
        data: getAsyncMenus()
      };
    }
  }
]);
